from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any


class TableOptionInfo(BaseModel):
    """表选项信息模型"""
    value: str = Field(..., description="表名（实际值）")
    label: str = Field(..., description="显示名称（表名+注释）")
    comment: Optional[str] = Field(None, description="表注释")


class TableColumnInfo(BaseModel):
    """表列信息模型"""
    column_name: str = Field(..., description="列名")
    data_type: str = Field(..., description="数据类型")
    is_nullable: bool = Field(..., description="是否可为空")
    column_default: Optional[str] = Field(None, description="默认值")
    column_comment: Optional[str] = Field(None, description="列注释")
    is_primary_key: bool = Field(False, description="是否为主键")
    is_auto_increment: bool = Field(False, description="是否自增")
    character_maximum_length: Optional[int] = Field(None, description="字符最大长度")
    numeric_precision: Optional[int] = Field(None, description="数值精度")
    numeric_scale: Optional[int] = Field(None, description="数值小数位数")


class TableIndexInfo(BaseModel):
    """表索引信息模型"""
    index_name: str = Field(..., description="索引名称")
    column_names: List[str] = Field(..., description="索引列名列表")
    is_unique: bool = Field(..., description="是否唯一索引")
    index_type: str = Field("BTREE", description="索引类型")


class TableForeignKeyInfo(BaseModel):
    """表外键信息模型"""
    constraint_name: Optional[str] = Field(None, description="约束名称")
    column_names: List[str] = Field(..., description="外键列名列表")
    referenced_table: str = Field(..., description="引用的表名")
    referenced_columns: List[str] = Field(..., description="引用的列名列表")
    on_delete: str = Field("RESTRICT", description="删除时的操作")
    on_update: str = Field("RESTRICT", description="更新时的操作")


class TableBasicInfo(BaseModel):
    """表基本信息模型"""
    table_name: str = Field(..., description="表名")
    table_comment: Optional[str] = Field(None, description="表注释")
    column_count: int = Field(0, description="列数量")
    index_count: int = Field(0, description="索引数量")
    foreign_key_count: int = Field(0, description="外键数量")
    engine: str = Field("InnoDB", description="存储引擎")
    charset: str = Field("utf8mb4", description="字符集")


class TableStructureInfo(BaseModel):
    """表结构详细信息模型"""
    table_name: str = Field(..., description="表名")
    table_comment: Optional[str] = Field(None, description="表注释")
    columns: List[TableColumnInfo] = Field(..., description="列信息列表")
    primary_keys: List[str] = Field(..., description="主键列名列表")
    indexes: List[TableIndexInfo] = Field(..., description="索引信息列表")
    foreign_keys: List[TableForeignKeyInfo] = Field(..., description="外键信息列表")
    column_count: int = Field(0, description="列数量")
    index_count: int = Field(0, description="索引数量")
    foreign_key_count: int = Field(0, description="外键数量")


class TableListResponse(BaseModel):
    """表列表响应模型"""
    total: int = Field(..., description="总数量")
    items: List[TableBasicInfo] = Field(..., description="表信息列表")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页大小")
    pages: int = Field(..., description="总页数")


class DatabaseSummaryInfo(BaseModel):
    """数据库概要信息模型"""
    total_tables: int = Field(0, description="总表数量")
    total_columns: int = Field(0, description="总列数量")
    total_indexes: int = Field(0, description="总索引数量")
    total_foreign_keys: int = Field(0, description="总外键数量")
    system_tables_count: int = Field(0, description="系统表数量")
    business_tables_count: int = Field(0, description="业务表数量")
    tables_by_type: Dict[str, int] = Field(default_factory=dict, description="按类型分组的表数量")


class TableSearchRequest(BaseModel):
    """表搜索请求模型"""
    keyword: Optional[str] = Field(None, description="搜索关键词")
    table_type: Optional[str] = Field(None, description="表类型：system/business")
    page: int = Field(1, description="页码", ge=1)
    size: int = Field(10, description="每页大小", ge=1, le=100)


class TableColumnSearchRequest(BaseModel):
    """表列搜索请求模型"""
    table_name: str = Field(..., description="表名")
    keyword: Optional[str] = Field(None, description="列名搜索关键词")